<template>
  <div class="custom-manage">
    <el-card>
      <h3>
        评论管理
        <!-- <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" circle
          @click="dialogFormVisible = true" style="right: 50px;"></el-button>
        <el-button type="primary" icon="el-icon-search" size="small" circle @click="search"></el-button> -->
        <el-input v-model="searchNickName" placeholder="姓名" style="width: 200px;float: right;margin: 0 10px 10px 10px;"></el-input>
        <el-input v-model="searchRoomNumber" placeholder="房间号" style="width: 200px; margin-right: 10px;float: right;margin: 0 10px 10px 10px;"></el-input>
      </h3>
      <el-table style="width: 100%" :data="filteredComments" border :cell-style="cellStyle">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" :index="indexMethod" width="80"></el-table-column>
        <el-table-column prop="roomNumber" label="房间号" width="80"> </el-table-column>
        <el-table-column prop="imgUrl" label="用户头像" width="120">
          <template slot-scope="scope">
            <img :src="scope.row.imgUrl" alt="" style="width: 100%;height: 50px;">
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="姓名" width="120"> </el-table-column>
        <el-table-column prop="comment" label="评论内容" width="300"> </el-table-column>
        <el-table-column prop="parentId" label="回复对象" width="100"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <!-- <el-button type="primary" size="small" icon="el-icon-edit" circle
              @click="editUser(scope.row.telephone)"></el-button> -->
            <el-button type="danger" size="small" circle icon="el-icon-delete"
              @click="deleteUser(scope.row.commentId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="请填写员工" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="手机号" :label-width="formLabelWidth" v-if="this.status === 0">
          <el-input v-model="form.telephone" autocomplete="off" placeholder="请输入手机号"
            value="this.form.telephone"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" v-else>
          <el-input v-model="form.telephone" autocomplete="off" placeholder="请输入手机号" value="this.form.telephone"
            disabled></el-input>
        </el-form-item>

        <el-form-item label="邮箱" :label-width="formLabelWidth" v-if="this.status === 0">
          <el-input v-model="form.email" autocomplete="off" placeholder="请输入邮箱" value="this.form.email"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" v-else>
          <el-input v-model="form.email" autocomplete="off" placeholder="请输入邮箱" value="this.form.email"
            disabled></el-input>
        </el-form-item>
        <el-form-item label="身份证号" :label-width="formLabelWidth">
          <el-input v-model="form.idCard" autocomplete="off" placeholder="请输入身份证号" type="number"
            value="this.form.idCard"></el-input>
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="form.nickname" autocomplete="off" placeholder="请输入昵称" value="this.form.vipType"></el-input>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth">
          <el-input v-model="form.age" autocomplete="off" placeholder="请输入年龄" type="number"
            value="this.form.price"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <!-- <el-input v-model="form.sex" autocomplete="off" placeholder="请输入性别" type="number"
            value="this.form.discountRate"></el-input> -->

          <el-radio v-model="form.sex" label="男">男</el-radio>
          <el-radio v-model="form.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="薪水" :label-width="formLabelWidth">
          <el-input v-model="form.salary" autocomplete="off" placeholder="请输入薪水" type="number"
            value="this.form.discountRate"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.status === 0">确 定</el-button>
        <el-button type=" primary" @click="updateType" v-else>更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCommentsList, allComments, deleteComment } from '@/api/comments'

export default {
  name: 'CommentManage',
  data() {
    return {
      currentPage: 1,
      total: 0,
      pageSizes: [],
      pageSize: 5,
      commentList: [],
      searchRoomNumber: '',
      searchNickName: '',
      dialogFormVisible: false,
      form: {
        email: '',
        telephone: '',
        idCard: '',
        nickname: '',
        age: '',
        sex: '',
        salary: '',
        imgUrl: '',
        state: '',
      },
      formLabelWidth: '120px',
      status: 0,
    }
  },

  watch: {
    dialogFormVisible(val) {
      if (val === false) {
        this.status = 0
      }
    }
  },
  computed: {
    filteredComments() {
      return this.commentList.filter(comment => {
        return (
          (!this.searchRoomNumber || comment.roomNumber.toString().includes(this.searchRoomNumber)) &&
          (!this.searchNickName || comment.nickName.includes(this.searchNickName))
        );
      }).slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
    },
  },
  created() {
    for (let i = 0; i < 5; i++) {
      this.pageSizes.push(5 * (i + 1))
    }
    this.fetchData()
  },
  methods: {
    fetchData() {
      allComments().then(res => {
        this.commentList = res
        console.warn(this.commentList)
        this.total = this.commentList.length
        const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' }
        for (let i = 0; i < this.commentList.length; i++) {
          if (this.commentList[i].parentId === 0) {
            this.commentList[i].parentId = '无'
          } else {
            for (let j = 0; j < this.commentList.length; j++) {
              if (this.commentList[i].parentId === this.commentList[j].commentId) {
                this.commentList[i].parentId = this.commentList[j].nickName
              }
            }
          }
          // this.vipList[i].imgUrl = process.env.VUE_APP_IMG_PREFIX + this.vipList[i].imgUrl
          this.commentList[i].createTime = new Date(this.commentList[i].createTime).toLocaleDateString(undefined, options)
        }
      })
    },
    add() {
      if (this.form.telephone === '' || this.form.nickname === '' || this.form.salary === '' || this.form.age === '' || this.form.sex === '' || this.form.email === '' || this.form.idCard === '') {
        this.$message('内容没写全')
      } else {
        console.log(this.form)
        createEmployee(this.form).then(res => {
          this.dialogFormVisible = false
          this.fetchData()
          this.$notify({
            title: '成功',
            message: '员工创建成功',
            type: 'success'
          })
          this.form = {}
        })
      }
    },
    deleteUser(commentId) {
      this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.error(commentId)
        deleteComment(commentId).then(res => {
          console.log(res)
          this.fetchData()
          this.currentPage = 1
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    search() {
      this.$prompt('请输入电话号搜索', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        for (let i = 0; i < this.commentList.length; i++) {
          if (this.commentList[i].telephone === value) {
            this.$message({
              type: 'success',
              message: '搜索到该会员'
            });
            this.commentList = [this.commentList[i]]
            return
          }
        }
        this.$message({
          type: 'error',
          message: '没有搜索到该会员'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'vipType') {
        switch (row.vipType) {
          case '黄金':
            return { color: '#FFD700', fontWeight: 'bold' }; // 金色
          case '白银':
            return { color: '#C0C0C0', fontWeight: 'bold' }; // 银色
          case '青铜':
            return { color: '#CD7F32', fontWeight: 'bold' }; // 铜色
          case '铂金':
            return { color: '#008899', fontWeight: 'bold' }; // 铂金色
          case '钻石':
            return { color: '#0094c8', fontWeight: 'bold' }; // 钻石色
          default:
            return {};
        }
      }
      return {};
    },
    //切换每页条数容量
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.fetchData()
    },
    //切换当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    indexMethod(index) {
      return index + 1 + (this.currentPage - 1) * this.pageSize;
    },
    open() {
      window.open(process.env.VUE_APP_BASE_URL + '/excel/employee/export')
    },
  }
}
</script>

<style lang="less" scoped>
.custom-manage {
  display: flex;
  justify-content: center;
}

.el-card {
  display: flex;
  flex-direction: column;
  width: 94%;
  margin: 20px 0;

  h3 {
    position: relative;
    height: 35px;

    .el-button {
      position: absolute;
      right: 10px;
      top: 5;
    }
  }

  .el-table {
    width: 100%;
    text-align: center;

    .el-table-column {
      text-align: center;
    }
  }
}

.el-pagination {
  margin: 10px 0 10px 300px;
}
</style>
